#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QString>
#include<QDebug>
#include<QPaintEvent>
#include<QSound>
#include<smoke.h>
#include<QCoreApplication>
#include<QGraphicsItem>


int MainWindow::random(int a,int b)
{
    qint16 rand= QRandomGenerator::global()->bounded(a,b);//获取a-b的随机数
    return rand;

}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    paintEvent(this->e);
    in();//初始化
    game();//初始化游戏
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::in()
{
    //QSound::play();//播放音乐
    this->setFixedSize(1500,900);
    qDebug()<<"初始化";
    QPainter* painter=new QPainter(this);

    QPixmap ground("://Saved Pictures/xingqiong.png");
    ground=ground.scaled(1500,900);//缩放
    QPixmap back("://Saved Pictures/dibupicture.png");
    back=back.scaled(1500,120);//缩放
    painter->drawPixmap(0,0,ground);


    int h=random(0,100);
//    if(Cigarettes==nullptr)
//    {
//        Cigarettes=new QVector<Smoke*>(2);//创建两个香烟
//    }
//    else
//    {
//        delete (*Cigarettes)[0];
//        delete (*Cigarettes)[1];
//    }
//    (*Cigarettes)[0]=new Smoke(height(),100+h,width()+100,ground.height(),this);
//    (*Cigarettes)[1]=new Smoke(height(),150+h,2*width()+h,ground.height(),this);

    Smoke s;
    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    Smoke *S=new Smoke();
    S->insmoke();
    painter->end();
    delete S;
    delete painter;
}


void MainWindow::smokedraw()
{
        QPainter* painter=new QPainter(this);
        int h=random(0,100);
        int _2X=0;//第二个香烟x坐标
        Smoke* SM=new Smoke();
//        for(int i = 0; i < Cigarettes->size(); i++)
//            {
//                Smoke * smo = (*Cigarettes)[i];
//                if(i ==0)
//                {
//                    _2X = (*Cigarettes)[1]->getx();
//                }else {
//                    _2X = (*Cigarettes)[0]->getx();
//                }

        static int x1=1200;
        if(x1>0)
        {
            x1-=10;
            SM->setx(x1);
            SM->draw(*painter);

        }
        else
        {
            x1=1400;//设置位置
//            int h=random(10,700);
            SM->seth1(h);//设置高度
//            qDebug()<<h;
            SM->insmoke();//初始化
        }
        if(((x1<=540&&x1>=410)&&(SM->h1>=dzy))||((x1<=540&&x1>=410)&&(425-SM->h1<=dzy)))
        {
            x1=500;
            endgame();
            i=0;
        }
        painter->end();
        delete painter;
        delete SM;
}


void MainWindow::speed()
{
    downSpeed=1.0;
    upSpeed=20.0;
}

void MainWindow::game()
{
    in();
    speed();
    dzStatus=DZ::down;
    timer=new QTimer(this);
    timer->start(16);

}

void MainWindow::endgame()
{
    QPainter* painter=new QPainter(this);
    dz3=new QPixmap("://Saved Pictures/dingzhencry.png");
    painter->drawPixmap(100,100,*dz3);
    timer->stop();
    gameStatus=GAME::STOP;
    downSpeed=0;
    upSpeed=0;
    QPushButton* over=new QPushButton("游戏结束");
    over->move(1000,400);
    over->resize(500,300);
    over->setFont(QFont("隶书",24));
//    over->setParent(this);
//    over.raise();
    over->show();
    setUpdatesEnabled(false);
    connect(over,&QPushButton::clicked,this,&MainWindow::click_);
    delete painter;

}

void MainWindow::paintEvent(QPaintEvent *ev)
{
    QPainter* painter=new QPainter(this);
    in();
    qDebug()<<"绘图";
    backdraw();
    smokedraw();
    dzdraw();//绘制背景图，芙蓉王障碍，丁真角色
    update();
    delete painter;
}

void MainWindow::backdraw()//绘制背景
{
    QPainter* painter=new QPainter(this);
    QImage back("://Saved Pictures/dibupicture.png");
    back=back.scaled(1500,120);//缩放
    if(i<-1200)
    {
        i=0;
        painter->drawImage(i,780,back);
        i-=10;
        painter->drawImage(1200+i,780,back);
    }
    else
    {
        painter->drawImage(i,780,back);
        i-=10;
        painter->drawImage(1200+i,780,back);
    }
    painter->end();
    delete painter;
}

void MainWindow::dzdraw()//绘制角色
{

    dz1=new QPixmap("://Saved Pictures/dingzhen.png");
    dz3=new QPixmap("://Saved Pictures/dingzhencry.png");

    QPainter* painter=new QPainter(this);

    QPixmap dz("://Saved Pictures/dingzhen.png");//绘制丁真
    dz=dz.scaled(40,55);//缩放

    if(dzStatus==DZ::down)
    {
        dzy+=downSpeed;//实现下降绘制
        downSpeed+=0.2;//下降加速度

    }
    if(dzStatus==DZ::up)
    {
        dzy=dzy-upSpeed;
        upSpeed-=2.0;
        if(upSpeed<=0.0)
        {
            dzStatus=DZ::down;
            speed();
        }
    }

    dzx=500;
    painter->drawPixmap(dzx,dzy,dz);
    if(dzy>=780||dzy<=0)
    {
        endgame();
    }
    painter->end();
    delete painter;
}

void MainWindow::mousePressEvent(QMouseEvent *)
{
    qDebug()<<"点击";
    //QSound::play(WINGPATH);//添加声音，每次点击
    dzStatus=DZ::up;
    in();
    speed();
}

void MainWindow::cirgame()
{
    game();
}
